package com.game77.guessTheWords2.scene;

import android.view.KeyEvent;

import com.game77.guessTheWords2.MainActivity;
import com.game77.guessTheWords2.core.StageManager;
import com.game77.guessTheWords2.layer.GameLayer;
import com.game77.guessTheWords2.layer.ToolShopLayer;
import com.game77.guessTheWords2.util.ADHelper;
import com.game77.guessTheWords2.util.GameUtil;
import com.wiyun.engine.nodes.Director;
import com.wiyun.engine.nodes.Scene;

public class ToolShopScene extends Scene {
	private static ToolShopScene instance = null;
	private static Object sync_obj = new Object();

	ToolShopLayer layer;

	protected ToolShopScene() {
		layer = new ToolShopLayer();
		layer.setTouchEnabled(true);
		addChild(layer);
	}

	public static ToolShopScene make() {
		synchronized (sync_obj) {
			if (instance == null) {
				instance = new ToolShopScene();
			}
			MainActivity.instance.isLayerLoaded = true;
			return instance;
		}
	}

	public void updateCoins() {
		Director.getInstance().runOnGLThread(new Runnable() {

			@Override
			public void run() {
				layer.updateCount();
			}
		});

	}

	/**
	 * 1-游戏界面 2-章节界面
	 */
	public int LastScene = 0;

	public void returnLastScene() {
		if (LastScene == 1) {
			// 进入游戏界面，继续游戏
			GameUtil.switchSceneFlipX(GameScene.make());
			GameLayer.instance.startGame(StageManager.instance.getLastStage());
			// 显示广告
			ADHelper.showAd();
		} else if (LastScene == 2) {
			GameUtil.switchSceneNormalFast(ClassChooseScene.make());
			ClassChooseScene.make().updateCoins();
		}
	}

	@Override
	public boolean wyKeyDown(KeyEvent event) {

		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
			returnLastScene();
			return true;
		}
		return super.wyKeyDown(event);
	}
}
